package com.metaera.app.utils;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

/**
 * Desc: 时间计数器
 */
public class LifecycleCountDownTimer extends CountDownTimer implements DefaultLifecycleObserver {

    private static final long MILLIS_IN_FUTURE = 60 * 1000,
            COUNT_DOWN_INTERVAL = 1000;

    private TextView mTextView;
    private String mText, mFormat;

    public LifecycleCountDownTimer(Lifecycle lifecycle, TextView view) {
        this(lifecycle, view, null);
    }

    public LifecycleCountDownTimer(Lifecycle lifecycle, TextView view, String format) {
        super(MILLIS_IN_FUTURE, COUNT_DOWN_INTERVAL);
        mTextView = view;
        mFormat = format;
        mText = view.getText().toString();

        lifecycle.addObserver(this);
    }

    @Override
    public void onFinish() {
        mTextView.setText(mText);
        mTextView.setEnabled(true);
    }

    @SuppressLint("StringFormatMatches")
    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setEnabled(false);

        long args = millisUntilFinished / COUNT_DOWN_INTERVAL;

        if (mFormat == null){
            mTextView.setText(String.valueOf(args));

        } else {
            mTextView.setText(String.format(mFormat, args));
        }
    }


    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        cancel();
    }

}
